//
//  ChartView.m
//  ChartDemoX
//
//  Created by yuanpt on 17/5/6.
//  Copyright © 2017年 yuanpt. All rights reserved.
//

#import "ChartView.h"
#import "ChartYAxisView.h"
#import "ChartUtil.h"
#import "UIView+Extension.h"
#import "ChartDrawView.h"
#import "UIColor+Extensions.h"
#import "ChartXAxisView.h"
#import "ChartLine.h"
#import "PopoverView.h"

@interface ChartView()
<
UIScrollViewDelegate
>

@end

@implementation ChartView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) return nil;
    [self setupGestureRecognizer];
    [self addSubview:self.yAxisTitleLabel];
    [self addSubview:self.scrollView];
    [self addSubview:self.yAxisView];
    [self.scrollView addSubview:self.drawView];
    [self addSubview:self.xAxisView];
    [self addSubview:self.xAxisTitleLabel];
    [self.scrollView addSubview:self.popoverView];
    [self layout];
    return self;
}


- (void)setupGestureRecognizer {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedInChart:)];
    [self addGestureRecognizer:tap];
}

- (void)tappedInChart:(UITapGestureRecognizer *)tgr {
    CGPoint point = [tgr locationInView:self.drawView];
    NSLog(@"%@", NSStringFromCGPoint(point));
    CGFloat width = 12;
    CGFloat height = 18;
    CGRect frame = CGRectMake(point.x - width * 0.5, point.y - height * 0.5, width, height);
    __block BOOL showPopoverView = NO;
    [self.recordLine.points enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGPoint p = [obj CGPointValue];
        showPopoverView = CGRectContainsPoint(frame, p);
        if (showPopoverView) {
            *stop = showPopoverView;
            self.popoverView.sy_centerX = p.x;
            self.popoverView.sy_maxY = p.y;
        }
        
    }];
    self.popoverView.hidden = !showPopoverView;
}

- (UIBezierPath *)closedPath {
    NSArray *max = self.maxLine.points;
    NSArray *min = self.minLine.points;
    
    CGPoint fitst = [max.firstObject CGPointValue];
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:fitst];
    
    for (NSInteger i = 1; i < max.count; ++i) {
        [path addLineToPoint:[max[i] CGPointValue]];
    }
    
    CGPoint minLastPonit = [min.lastObject CGPointValue];
    [path addLineToPoint:minLastPonit];
    
    for (NSInteger i = min.count - 1 -1; i >= 0; --i) {
        [path addLineToPoint:[min[i] CGPointValue]];
    }
    
    [path addLineToPoint:[max.firstObject CGPointValue]];
    [path closePath];
    return path;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.xAxisView updateLabelPositonWithOffsetX:scrollView.contentOffset.x];
}

- (void)layout {
    self.popoverView.frame = CGRectMake(0, 0, 66 * scale(), 31 * scale());
    
    self.yAxisTitleLabel.frame = CGRectMake(15, 0, 150, 16);
    self.yAxisView.frame = CGRectMake(0, self.yAxisTitleLabel.sy_maxY, 45, 384 * scale());
    
    self.scrollView.sy_x = self.yAxisView.sy_maxX;
    self.scrollView.sy_y = self.yAxisView.sy_y;
    self.scrollView.sy_width = self.sy_width - 20 - self.yAxisView.sy_maxX;
    self.scrollView.sy_height = self.yAxisView.sy_height;
    self.scrollView.contentSize = CGSizeMake(1530  * scale(), 384 * scale());
    
    
    self.drawView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
    
    self.xAxisView.sy_width = self.scrollView.sy_width;
    self.xAxisView.sy_height = 25;
    self.xAxisView.sy_x = self.scrollView.sy_x;
    self.xAxisView.sy_y = self.scrollView.sy_maxY;
    
    self.xAxisTitleLabel.sy_width = self.xAxisView.sy_width;
    self.xAxisTitleLabel.sy_height = 16.0f;
    self.xAxisTitleLabel.center = self.xAxisView.center;
    self.xAxisTitleLabel.sy_y = self.xAxisView.sy_maxY;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self layout];
    [self.xAxisView updateLabels];
}

- (ChartYAxisView *)yAxisView {
    if (!_yAxisView) {
        _yAxisView = [[ChartYAxisView alloc] init];
        _yAxisView.chartView = self;
    }
    return _yAxisView;
}

- (void)setYAxisTitle:(NSString *)yAxisTitle {
    _yAxisTitle = yAxisTitle;
    self.yAxisTitleLabel.text = yAxisTitle;
}

- (UILabel *)yAxisTitleLabel {
    if (!_yAxisTitleLabel) {
        _yAxisTitleLabel = [[UILabel alloc] init];
        _yAxisTitleLabel.font = [UIFont systemFontOfSize:11.0];
    }
    return _yAxisTitleLabel;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.layer.borderColor = [UIColor sy_chartLineColor].CGColor;
        _scrollView.layer.borderWidth = 1.0;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.alwaysBounceHorizontal = NO;
        _scrollView.alwaysBounceVertical = NO;
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

- (ChartDrawView *)drawView {
    if (!_drawView) {
        _drawView = [[ChartDrawView alloc] init];
        _drawView.chartView = self;
    }
    return _drawView;
}

- (ChartXAxisView *)xAxisView {
    if (!_xAxisView) {
        _xAxisView = [[ChartXAxisView alloc] init];
        _xAxisView.chartView = self;
    }
    return _xAxisView;
}

- (UILabel *)xAxisTitleLabel {
    if (!_xAxisTitleLabel) {
        _xAxisTitleLabel = [[UILabel alloc] init];
        _xAxisTitleLabel.textAlignment = NSTextAlignmentCenter;
        _xAxisTitleLabel.font = [UIFont systemFontOfSize:11.0];
    }
    return _xAxisTitleLabel;
}

- (void)setXAxisTitle:(NSString *)xAxisTitle {
    _xAxisTitle = xAxisTitle;
    self.xAxisTitleLabel.text = xAxisTitle;
}

- (void)setMinLine:(ChartLine *)minLine {
    _minLine = minLine;
    [self.drawView setNeedsDisplay];
}

- (void)setMaxLine:(ChartLine *)maxLine {
    _maxLine = maxLine;
    [self.drawView setNeedsDisplay];
}

- (void)setNormalLine:(ChartLine *)normalLine {
    _normalLine = normalLine;
    [self.drawView setNeedsDisplay];
}

- (void)setRecordLine:(ChartLine *)recordLine {
    _recordLine = recordLine;
    [self.drawView setNeedsDisplay];
}

- (PopoverView *)popoverView {
    if (!_popoverView) {
        _popoverView = [[PopoverView alloc] init];
        _popoverView.hidden = YES;
    }
    return _popoverView;
}

@end
